<template>
	<view class="myOrder">
		<u-sticky bgColor="#fff">
			<u-tabs
				:current="current"
				:scrollable="false"
				:list="tabNav"
				@click="clickTab"
				inactiveStyle="font-size: 24rpx;"
				lineColor="#E54E4E"
				itemStyle="height: 88rpx;white-space: nowrap;"
			></u-tabs>
		</u-sticky>
		<view class="container">
			<j-orderList :list="list" @handleItem="handleItem" @btns="clickbtn"></j-orderList>
			<u-loadmore :status="status" />
		</view>
		<u-modal :show="modal" title="确定取消该订单吗？" @close="modal = false" :closeOnClickOverlay="true">
			<view class="u-modal-btnbox" slot="confirmButton">
				<button @click="modal = false">取消</button>
				<button @click="del">确认</button>
			</view>
		</u-modal>
	</view>
</template>

<script>
import { get_userInfo } from '@/common/common_fun.js';
import API from '@/common/api';
export default {
	name: 'myOrder',
	data() {
		return {
			current: 0,
			modal: false,
			status: '',
			tabNav: [
				{
					name: '全部'
				},
				{
					name: '待付款',
					key: 'order_need_pay',
					badge: {
						value: 0
					}
				},
				{
					name: '待发货',
					key: 'order_need_delivery',
					badge: {
						value: 0
					}
				},
				{
					name: '待收货',
					key: 'order_need_receive',
					badge: {
						value: 0
					}
				},
				{
					name: '待评价',
					key: 'order_need_pingjia',
					badge: {
						value: 0
					}
				}
			],
			list: [],
			tempItem: {},
			paging: {
				page: 1,
				pagenum: 10
			}
		};
	},
	onLoad(data) {
		if (data.hasOwnProperty('current')) {
			this.current = Number(data.current);
		}
	},
	onShow(data) {
		this.reset();
		this.init();
	},
	onReachBottom() {
		if (this.status === 'nomore') return;
		this.init();
	},
	methods: {
		handleItem(item) {
			this.toNav(`goods/goodsDetail?id=${item.id}&inventoryId=${item.id}&num=${item.num}`);
		},
		updateTag() {
			get_userInfo((data) => {
				let numberKeys = ['order_need_pay', 'order_need_delivery', 'order_need_receive', 'order_need_pingjia', 'yhq_num'];
				for (const key of numberKeys) {
					this.tabNav.forEach((item, i) => {
						if (item.key && item.key === key) this.tabNav[i].badge.value = data[key];
					});
				}
			});
		},
		qr() {
			API.getJson('qxOrder', { id: this.litem.id }, 'POST').then((res) => {
				const { code, data, msg } = res;
				if (code === 200) {
					uni.showToast({
						title: '取消成功',
						icon: 'success'
					});
					this.setState(this.current);
					this.qxpop = false;
				}
			});
		},
		clickbtn(flag, item) {
			this.tempItem = item;

			switch (flag) {
				case 0:
					this.j_modal(
						'确认取消订单吗？',
						() => {
							this.qx_order(item);
						},
						true
					);
					break;
				case 1:
					this.toNav(`order/orderDetails?id=${item.id}`);
					break;
				case 2:
					this.qxpop = true;
					break;
				case 3:
					//拉起确认收货组件
					
					if(item.transaction_id){
						uni.showLoading({
							title: '加载中',
							mask: true
						});
						if (wx.openBusinessView) {
							wx.openBusinessView({
								businessType: 'weappOrderConfirm',
								extraData: {
									transaction_id: item.transaction_id
								},
								success: (res) => {
									console.log(res.extraData.status, 'successextraData');
									if (res.extraData.status == 'success') {
										console.log('status--success');
										this.qrshouhuo(item.id);
									}
									if (res.extraData.status == 'cancel') {
										console.log('status--cancel');
										this.msg('已取消');
									}
									if (res.extraData.status == 'fail') {
										console.log('status--fail');
										this.msg('确认收货失败');
									}
								},
								fail: (err) => {
									//dosomething
									console.log(err, 'fail');
								},
								complete: () => {
									//dosomething
									uni.hideLoading();
									console.log('complete');
								}
							});
						} else {
							//引导用户升级微信版本
							uni.showToast({
								title: '您的微信版本过低，请升级微信版本',
								icon: 'none'
							});
						}
					}else{
						uni.showModal({
							title: '确定到货了吗',
							success: (res) => {
								if (res.confirm) {
									this.qrshouhuo(item.id);
								}
							}
						});
					}

					break;
				case 4:
					this.toNav(`pinglun/pinglun?id=${item.id}`);
					break;
				case 5:
					this.j_modal(
						'确认删除订单吗？',
						() => {
							this.qrdel(item.id);
						},
						true
					);
					break;
				default:
					break;
			}
		},
		qrdel(order_id) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			this.$http.getJson('orders_del', { order_id }).then((res) => {
				uni.hideLoading();
				if (res.code == 1) {
					this.reset();
					this.init();
					this.msg('操作成功');
				} else {
					this.msg(res.message);
				}
			});
		},
		qrshouhuo(order_id) {
			console.log(order_id, 'order_id');
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			this.$http.getJson('orders_qrshouhuo', { order_id }).then((res) => {
				uni.hideLoading();
				if (res.code == 1) {
					this.reset();
					this.init();
					this.msg('确认收货成功');
				} else {
					this.msg(res.message);
				}
			});
		},

		qx_order(item) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			this.$http.getJson('orders_qxOrder', { order_id: item.id }).then((res) => {
				uni.hideLoading();
				if (res.code == 1) {
					this.msg('操作成功');
					this.reset();
					this.init();
				} else {
					this.msg(res.message);
				}
			});
		},

		init(status = 0) {
			this.updateTag();
			this.status = 'loading';
			API.getJson('orders_lists', { ...this.paging, scene: this.current }).then((res) => {
				const { code, data, message } = res;
				if (code === 1) {
					if (data.length !== 0) this.list = this.list.concat(data);
					if (this.paging.pagenum === data.length) {
						this.paging.page += 1;
						this.status = 'loadmore';
					} else {
						this.status = 'nomore';
					}
				}
			});
		},
		reset() {
			this.paging.page = 1;
			this.list = [];
		},
		clickTab(e) {
			this.current = e.index;
			this.reset();
			this.init();
		}
	}
};
</script>

<style lang="less" scoped>
.myOrder {
	.u-modal-btnbox {
		button {
			border-radius: 8rpx 8rpx 8rpx 8rpx;
		}
	}
	.container {
		padding: 24rpx;
	}
}
</style>
